//
//  YLMineViewModel.m
//  YLFinanceService
//
//  Created by she1323 on 2017/10/25.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLMineViewModel.h"
#import "WHNetworkTool.h"
#import "WHProgressHUD.h"
#import "YLAccountTool.h"
#import "YLMyInfoViewModel.h"
#import "YLMyInfoModel.h"
#import <YYModel.h>
#import "YLSwitchRootController.h"
@implementation YLMineViewModel


- (instancetype)init
{
    if (self = [super init]) {
    }
    return self;
}

- (RACCommand *)myInfoCommand
{
    if (!_myInfoCommand) {
        
        
        __weak typeof(self) weakSelf = self;
        _myInfoCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                
                NSString *url = [NSString stringWithFormat:@"%@AppCommon/PGetCustomerRegInfo", YLURL];
                
                [[WHNetworkTool shareInstance] sendPostWithUrl:url header:[YLAccountTool header] params:nil success:^(id responseObject) {
                    [WHProgressHUD dismiss];
                    if ([responseObject[@"httpCode"] integerValue] == 200) {
                        YLMyInfoModel *model = [YLMyInfoModel yy_modelWithDictionary:responseObject[@"httpResult"]];
                        weakSelf.myInfoModel = model;
                        YLAccount *account = [YLAccountTool account];
                        account.identityID = model.identityID;
                        account.authName = model.authName;
                        account.fundRiskGrade = model.fundRiskGrade;
                        account.authState = model.authState;
                        account.isPassed = model.isPassed;
                        account.pointQuota = model.pointQuota;
                        account.shareUrl = model.shareUrl;
                        account.workerNum = model.workerNum;
                        account.workerName = model.workerName;
                        account.workerPhone = model.workerPhone;
                        account.tag = model.tag;
                        account.alias = model.alias;
                        account.fundQualifications = model.fundQualifications;
                        [YLAccountTool saveAccount:account];
                        [subscriber sendNext:nil];
                        
                    } else {
                        [WHProgressHUD showError:responseObject[@"message"]];
                        [weakSelf.wh_loadDataErrorSubject sendNext:nil];
                    }
                    
                    [subscriber sendCompleted];
                } fail:^(NSError *error) {
                    [weakSelf.wh_loadDataErrorSubject sendNext:nil];
                    [subscriber sendCompleted];
                }];
                
                
                return nil;
            }];
            
        }];
    }
    return _myInfoCommand;
}

- (RACCommand *)requestCommand
{
    if (!_requestCommand) {
        __weak typeof(self) weakSelf = self;
        _requestCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                
                NSString *url = [NSString stringWithFormat:@"%@AppCommon/PGetCustomerRegInfo", YLURL];
                
                [[WHNetworkTool shareInstance] sendPostWithUrl:url header:[YLAccountTool header] params:nil success:^(id responseObject) {
                    
                    if ([responseObject[@"httpCode"] integerValue] == 200) {
                        weakSelf.myInfoModel = [YLMyInfoModel yy_modelWithDictionary:responseObject[@"httpResult"]];
                        
                        YLAccount *account = [YLAccountTool account];
                        account.identityID = weakSelf.myInfoModel.identityID;
                        account.authName = weakSelf.myInfoModel.authName;
                        account.fundRiskGrade = weakSelf.myInfoModel.fundRiskGrade;
                        account.authState = weakSelf.myInfoModel.authState;
                        account.isPassed = weakSelf.myInfoModel.isPassed;
                        account.shareUrl = weakSelf.myInfoModel.shareUrl;
                        account.workerNum = weakSelf.myInfoModel.workerNum;
                        account.workerName = weakSelf.myInfoModel.workerName;
                        account.workerPhone = weakSelf.myInfoModel.workerPhone;
                        account.alias = weakSelf.myInfoModel.alias;
                        account.tag = weakSelf.myInfoModel.tag;
                        account.fundQualifications = weakSelf.myInfoModel.fundQualifications;
                        [YLAccountTool saveAccount:account];
                        
                        YLIconTitleModelGroup *group = weakSelf.groups.firstObject;
                        YLIconTitleNumberModel *model =  group.models[1];
                        
                        model.number = weakSelf.myInfoModel.continuedInvestmentOrderCount;
                        
                        if (weakSelf.myInfoModel.fundRiskGrade != 0) {
                            YLIconTitleModelGroup *lastGroup = weakSelf.groups.lastObject;
                            YLIconTitleContentModel *model = lastGroup.models.lastObject;
                            model.content = weakSelf.myInfoModel.fundRiskGradeText;
                            model.clickBlock = ^NSString *{
                                return @"YLEvaluationResultController";
                            };
                        }
                        [subscriber sendNext:nil];
                        
                    } else {
                        [WHProgressHUD showError:responseObject[@"message"]];
                        [weakSelf.wh_loadDataErrorSubject sendNext:nil];
                    }
                    
                    [subscriber sendCompleted];
                } fail:^(NSError *error) {
                    [weakSelf.wh_loadDataErrorSubject sendNext:nil];
                    [subscriber sendCompleted];
                }];
                

                return nil;
            }];
            
        }];
    }
    return _requestCommand;
}




- (NSArray<YLIconTitleModelGroup *> *)groups
{
    if (!_groups) {
        _groups = [[NSMutableArray array] init];
        // 第一组
        {
            YLIconTitleModelGroup *group = [[YLIconTitleModelGroup alloc] init];
            [group.models addObject:[YLIconTitleModel modelWithIcon:@"regularFinancing" title:@"普惠" clickBlock:^NSString *{
                return @"YLRegularFinancingViewController";
            }]];
            
            [group.models addObject:[YLIconTitleNumberModel modelWithIcon:@"renewalOrder" title:@"可续投订单" number:0 clickBlock:^NSString *{
                return @"YLContinuationOrderController";
            }]];
            
            [group.models addObject:[YLIconTitleModel modelWithIcon:@"appointmentRecord" title:@"预约记录" clickBlock:^NSString *{
                return @"YLReservationRecordController";
            }]];
        
            [_groups addObject:group];
        }
        
        // 第二组
        {
            YLIconTitleModelGroup *group = [[YLIconTitleModelGroup alloc] init];
            [group.models addObject:[YLIconTitleModel modelWithIcon:@"bindingInformation" title:@"绑定信息" clickBlock:^NSString *{
                return @"YLBindingInformationController";
            }]];
            
            [group.models addObject:[YLIconTitleModel modelWithIcon:@"accountSettings" title:@"账号设置" clickBlock:^NSString *{
                return @"YLAccountSettingController";
            }]];
            
            
            [group.models addObject:[YLIconTitleSpotModel modelWithIcon:@"messageCenter" title:@"消息" haveNews:NO clickBlock:^NSString *{
                return @"YLMessageCenterController";
            }]];
            [_groups addObject:group];
        }
        
        // 第三组
        {
            YLIconTitleModelGroup *group = [[YLIconTitleModelGroup alloc] init];
            [group.models addObject:[YLIconTitleModel modelWithIcon:@"myWelfare" title:@"我的福利" clickBlock:^NSString *{
                return @"YLMyWelfareViewController";
            }]];
            [group.models addObject:[YLIconTitleModel modelWithIcon:@"invitingFriends" title:@"邀请好友" clickBlock:^NSString *{
                return @"YLInvitingFriendsController";
            }]];
            [group.models addObject:[YLIconTitleContentModel modelWithIcon:@"riskAssessment" title:@"风险测评" content:@"点击测评" clickBlock:^NSString *{
                return @"YLRiskAssessmentController";
            }]];
            [_groups addObject:group];
        }
    }
    return _groups;
}

@end
